clear all
close all
load MCClrgb.mat
imageSize = 50;

rgbFLOUR = [217 166 162; 202 160 158; 182 140 140; 151 111 110; 107 74 72; 54 40 28; 18 29 85; 83 118 66; 232 0 0; 255 173 18; 255 48 122; 52 148 176; 206 50 0; 68 59 116; 232 20 32; 118 30 74; 195 173 78; 255 141 0; 100 23 12; 193 87 75; 96 93 112; 97 84 35; 156 109 142; 131 171 160];
rgbTUNGSTEN = [167 172 176; 151 158 164; 129 136 142; 129 136 142; 63 72 75; 24 26 25; 8 27 101; 40 106 64; 153 8 11; 197 173 24; 176 68 132; 18 132 195; 142 63 0; 29 58 136; 158 40 57; 64 35 82; 137 162 67; 193 142 0; 44 25 13; 132 92 89; 49 88 127; 50 75 33; 100 104 152; 83 159 165];
lrgbValuesMCCTung = (rgbTUNGSTEN./max(max(rgbTUNGSTEN))).^1.8;
CorrectionMatrix = lrgbValuesMCC'*pinv(lrgbValuesMCCTung');

[r, g, b]=fabricPatch(imread('fabricTungsten/red_satin.jpg'))
%[160 46 2; 171 10 69; 56 108 186;...
%	98 105 77; 114 80 70; 31 113 151;...
%	102 11 24; 143 69 8; 88 108 88;...
%	149 93 94; 186 1 2];
%imageDisplay([r g b])
rgbLin = ([r g b]./max(max(rgbTUNGSTEN))).^1.8;
rgbLinCorr = CorrectionMatrix*rgbLin';
rgbCorr = 256*(rgbLinCorr.^(1/2.2));
%imageDisplay(rgbCorr)
         
%trying to correct the whole image
fabr=imread('fabricTungsten/red_satin.jpg');
sz=size(fabr);
l=sz(1); w=sz(2);
for i=1:l
    for j=1:w
        r = double(fabr(i,j,1));
        g = double(fabr(i,j,2));
        b = double(fabr(i,j,3));
        fabrLin=([r g b]./max(max(rgbTUNGSTEN))).^1.8;
        fabrLinCorr=CorrectionMatrix*fabrLin';
        fabrCorr(i,j,1:3)=uint8(256*(fabrLinCorr.^(1/2.2)));
    end
end
figure
imshow(fabrCorr)
figure
imshow('fabricTungsten/red_satin.jpg')

% fleece: orange, pink, blue
% linen: green, brown, blue
% satin: red, yellow, green
% velvet: brown, red
rgbReal = [226 117 0; 187 69 120; 102 133 188;...
	148 152 113; 168 140 123; 121 192 197;...
	88 46 64; 214 163 94; 52 106 74;...
	97 78 71; 143 0 51]; 
rgbT = [160 46 2; 171 10 69; 56 108 186;...
	98 105 77; 114 80 70; 31 113 151;...
	102 11 24; 143 69 8; 88 108 88;...
	149 93 94; 186 1 2];
rgbCorrT = [209 89 79; 220 69 124; 96 146 211;...
	143 141 105; 160 117 105; 67 148 174;...
	145 47 67; 192 108 68; 132 143 115;...
	199 134 135; 235 69 93];

% blue fleece, green linen, yellow satin, red velvet
rgbRealF = [rgbReal(3,:);rgbReal(4,:);rgbReal(8,:);rgbReal(11,:)];
rgbF = [98 112 168; 132 91 60; 198 51 8; 240 1 1];
rgbCorrF = [103 145 217; 119 105 71; 176 90 77; 218 98 99];

errR = rgbReal(:,1)-rgbT(:,1); 
errG = rgbReal(:,2)-rgbT(:,2);
errB = rgbReal(:,3)-rgbT(:,3);
meanRGBT = mean(rgbReal'-rgbT');

errRcorr = rgbReal(:,1)-rgbCorrT(:,1);
errGcorr = rgbReal(:,2)-rgbCorrT(:,2);
errBcorr = rgbReal(:,3)-rgbCorrT(:,3);
meanRGBcorrT = mean(rgbReal'-rgbCorrT');

errRf = rgbRealF(:,1)-rgbF(:,1);
errGf = rgbRealF(:,2)-rgbF(:,2);
errBf = rgbRealF(:,3)-rgbF(:,3);
meanRGBF = mean(rgbRealF'-rgbF');

errRcorrF = rgbRealF(:,1)-rgbCorrF(:,1);
errGcorrF = rgbRealF(:,2)-rgbCorrF(:,2);
errBcorrF = rgbRealF(:,3)-rgbCorrF(:,3);
meanRGBcorrF = mean(rgbRealF'-rgbCorrF');

figure
scatter([1:11],abs(meanRGBT),'b*')
hold on
scatter([1:11],abs(meanRGBcorrT),'r*')
grid on
legend ('Error before correction','Error after correction')
title('Absolute Error with Tungsten Illumination Setting')
xlabel('Sample #')
ylabel('Absolute Error')
axis([0.5 11.5 0 90]);

figure
scatter([1:4],abs(meanRGBF),'b*')
hold on
scatter([1:4],abs(meanRGBcorrF),'r*')
grid on
axis([0.5 4.5 10 80]);

%figure
%bar([errR errRcorr errG errGcorr errB errBcorr])
%hold on
%bar(errG, 'g')
%bar(errB, 'b')
%bar(errRcorr, 'r-.')
%stem(errGcorr, 'g-.')
%stem(errBcorr, 'b-.')
%grid on

%figure
%plot([errRf errGf errBf])
%hold on
%plot([errRcorrF errGcorrF errBcorrF], '-.')
%grid on

% blue fleece, green linen, yellow satin, red velved
